package com.loong.musicdemo.ui.account;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.loong.musicdemo.AccountMyCenter;
import com.loong.musicdemo.LoginActivity;
import com.loong.musicdemo.PlaySongActivity;
import com.loong.musicdemo.R;
import com.loong.musicdemo.ui.account.MyCenter.MyFriends;
import com.loong.musicdemo.ui.mine.recent.PlayMvActivity;
import com.loong.musicdemo.util.httpUtil.HttpUtil;
import com.loong.musicdemo.util.httpUtil.LoginUtil;
import com.loong.musicdemo.widget.iconbtn.IconButton;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Properties;

import de.hdodenhof.circleimageview.CircleImageView;

import static android.content.Context.MODE_PRIVATE;

public class AccountFragment extends Fragment {
    private CircleImageView titleIcon;//头像
    private TextView titleName;//昵称

    //    private Button dailySignIn;//签到
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        //背景图片融合
        View decorView = requireActivity().getWindow().getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        requireActivity().getWindow().setStatusBarColor(Color.TRANSPARENT);
        View root = inflater.inflate(R.layout.fragment_account, container, false);
        initView(root);
        return root;
    }

    /**
     * 初始化布局
     *
     * @param root 根视图
     */
    private void initView(View root) {
        titleIcon = root.findViewById(R.id.title_ico);
        titleName = root.findViewById(R.id.title_name);
        //昵称
        TextView titleName = root.findViewById(R.id.title_name);
        IconButton myInfo = root.findViewById(R.id.account_my_info);
        myInfo.setName("我的消息")
                .setTextColor(Color.BLACK)
                .setIcon(R.drawable.account_my_info)
                .setOnClickListener(() -> {
                            Intent intent = new Intent(requireActivity(), PlaySongActivity.class);//调到歌曲显示页面
                            intent.putExtra("songId", "33894312");
                            startActivity(intent);
                        }
                );
        IconButton myFriends = root.findViewById(R.id.account_my_friends);
        myFriends.setName("我的好友")
                .setTextColor(Color.BLACK)
                .setIcon(R.drawable.account_my_friends)
                .setOnClickListener(() -> {
                    Intent intent = new Intent(requireActivity(), MyFriends.class);
                    startActivity(intent);//打开我的好友页面
                });
        IconButton myCenter = root.findViewById(R.id.account_my_center);
        myCenter.setName("个人主页")
                .setTextColor(Color.BLACK)
                .setIcon(R.drawable.account_my_center)
                .setOnClickListener(() -> {
                    Intent intent = new Intent(requireActivity(), AccountMyCenter.class);
                    startActivity(intent);
                });
        IconButton mySelf = root.findViewById(R.id.account_my_self);
        mySelf.setName("个性装扮")
                .setTextColor(Color.BLACK)
                .setIcon(R.drawable.account_my_self)
                .setOnClickListener(() -> Toast.makeText(getActivity(), "个性装扮功能暂未开放!", Toast.LENGTH_SHORT).show());
        /*退出登录*/
        root.findViewById(R.id.logout).setOnClickListener(v -> {
            LoginUtil.logout(requireActivity());
            Intent intent = new Intent(requireActivity(), LoginActivity.class);//调用退出登录方法
            startActivity(intent);//返回登录
            requireActivity().finish();//销毁当前活动
        });
        /*初始化头像和昵称*/
        SharedPreferences userInfo = requireActivity().getSharedPreferences("userInfo", MODE_PRIVATE);
        String userId;
        if ((userId = userInfo.getString("userId", null)) != null) {//当前存在登录信息
            titleName.setText(userInfo.getString("nickName", null));//加载昵称
            Properties pro = new Properties();
            try {
                pro.load(requireActivity().getAssets().open("httpconfig.properties"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            String host = pro.getProperty("host");
            String getUrl = "/user/detail?uid=" + userId;
            new Thread(() -> {
                String responseBody = HttpUtil.sendReturnOKHttpRequest(host + getUrl);
                try {
                    JSONObject userDetail = new JSONObject(responseBody);
                    JSONObject profile = userDetail.getJSONObject("profile");
                    String avatarUrl = profile.getString("avatarUrl");
                    requireActivity().runOnUiThread(() -> {
                        Glide.with(this).load(avatarUrl).into(titleIcon);
                    });
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            ).start();
        }

    }

    /**
     *
     */
    @Override
    public void onPause() {
        super.onPause();
    }
}
